import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import thunk from 'redux-thunk';

import loadingMiddleware from './middlewares/loadingMiddleware';
import promiseMiddleware from './middlewares/promiseMiddleware';
import errorMiddleware from './middlewares/errorMiddleware';
import api from './libs/api';

var composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
export default (function (reducer, history) {
  var middlewares = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

  var store = createStore(reducer, composeEnhancers(applyMiddleware.apply(undefined, [thunk.withExtraArgument(api), loadingMiddleware, promiseMiddleware, errorMiddleware, routerMiddleware(history)].concat(middlewares))));

  return store;
});